{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes       #-}
{-# LANGUAGE TemplateHaskell   #-}
{-# LANGUAGE TypeFamilies      #-}

module Chapter6b where

import Yesod
import Data.Time(getCurrentTime)

data App = App
mkYesod "App" [parseRoutes|
/ HomeR GET
/foo FooR GET
|]

instance Yesod App where
    defaultLayout contents = do
        PageContent title headTags bodyTags <- widgetToPageContent contents
        mmsg <- getMessage
        giveUrlRenderer [hamlet|
        $doctype 5
        <html>
            <head>
                <title>#{title}
                ^{headTags}
            <body>
                $maybe msg <- mmsg
                    <div #message>#{msg}
                ^{bodyTags}
        |]
getHomeR::Handler Html
getHomeR = do
    now <- liftIO getCurrentTime
    setMessage $ toHtml $ "You previously visited at: " ++ show now
    defaultLayout $ do
     [whamlet|<p> Try refreshing|]
     toWidgetHead $ toHtml [shamlet|<meta name=keywords content="home">|]

getFooR::Handler Html
getFooR = do
    now <- liftIO getCurrentTime
    setMessage $ toHtml $ "You previously visited at: " ++ show now
    defaultLayout $ do
     [whamlet|<p> Try refreshing|]
     toWidgetHead $ toHtml [shamlet|<meta name=keywords content="foor">|]

main :: IO()
main = warp 3000 App